#pragma once
#include<iostream>
#include<pthread.h>
#include<string>
#include<unistd.h>
#include<vector>

//先描述，把锁的功能描述起来

namespace MutexMoble
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_mutex,nullptr);
        }
        void Lock()  //上锁 
        {
            int n = pthread_mutex_lock(&_mutex);
            (void)n;  //避免没有使用到n导致编译器报错
        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);
            (void)n; //避免没有使用到n而导致编译器报错
        }
        
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }
    private:
        pthread_mutex_t _mutex;
    };

    //在组织，通过调用这个组织从而做到使用锁

    class LockGuard
    {
    public:
        LockGuard(Mutex &mutex):_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.Unlock(); //把锁给释放掉
        }
    private:
        Mutex  &_mutex;
    };
    
}